package me.boot.auth.jap.controller;

import com.fujieid.jap.core.JapUserService;
import com.fujieid.jap.core.config.AuthenticateConfig;
import com.fujieid.jap.core.config.JapConfig;
import com.fujieid.jap.social.SocialConfig;
import com.fujieid.jap.social.SocialStrategy;
import me.boot.auth.jap.config.AuthProperties;
import me.boot.auth.jap.config.AuthStrategy;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UuidUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 使用知名第三方平台的认证服务（类似OAuth2服务）
 * 需要依赖 jap-social 模块
 */
@RestController
@RequestMapping("/social")
public class SocialController extends BaseAuthController<SocialStrategy> {

    @Resource(name = "social")
    private JapUserService japUserService;

    @Override
    protected AuthStrategy getAuthStrategy() {
        return AuthStrategy.social;
    }

    @Override
    protected JapUserService getUserService() {
        return japUserService;
    }

    @Override
    protected SocialStrategy getJapStrategy(JapUserService japUserService, JapConfig japConfig) {
        return new SocialStrategy(japUserService, japConfig);
    }

    @Override
    protected AuthenticateConfig getAuthenticateConfig(AuthProperties.Platform platform) {
        return new SocialConfig()
                .setPlatform(platform.getPlatformId())
                .setState(UuidUtils.getUUID())
                .setJustAuthConfig(AuthConfig.builder()
                        .clientId(platform.getClientId())
                        .clientSecret(platform.getClientSecret())
                        .redirectUri(platform.getCallbackUrl())
                        .build());
    }
}
